set(CMAKE_SYSTEM_NAME               Generic)
# set(CMAKE_SYSTEM_PROCESSOR          arm)

set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)
set(CMAKE_C_COMPILER_ID GNU)
set(CMAKE_CXX_COMPILER_ID GNU)

# Some default GCC settings
# arm-none-eabi- must be part of path environment
set(TOOLCHAIN_PREFIX                )

set(CMAKE_C_COMPILER                ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER              ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER              ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_LINKER                    ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_OBJCOPY                   ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE                      ${TOOLCHAIN_PREFIX}size)

# set(CMAKE_EXECUTABLE_SUFFIX_ASM     ".elf")
# set(CMAKE_EXECUTABLE_SUFFIX_C       ".elf")
# set(CMAKE_EXECUTABLE_SUFFIX_CXX     ".elf")

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -g3")
endif()
if(CMAKE_BUILD_TYPE MATCHES Release)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -g0")
endif()

set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -MMD -MP")
# set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fno-rtti -fno-exceptions -fno-threadsafe-statics")

# set(CMAKE_C_LINK_FLAGS "${TARGET_FLAGS}")
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -T \"${CMAKE_SOURCE_DIR}/STM32F429IGTx_FLASH.ld\"")
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} --specs=nano.specs")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-Map=${CMAKE_PROJECT_NAME}.map -Wl,--gc-sections")
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group -lc -lm -Wl,--end-group")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group -lm -Wl,--end-group")
# set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--print-memory-usage")

set(CMAKE_CXX_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group -lstdc++ -lsupc++ -Wl,--end-group")


# add custom compile and linker flags
set(CUSTOM_C_CXX_FLAGS
    -Wall           # 启用所有警告
    -Wextra         # 启用额外的警告
    -Wpedantic      # 启用兼容性警告 要求严格遵循标准
    -Wno-unused-variable    # 禁用未使用的变量警告
    -Wno-unused-parameter   # 禁用未使用的参数警告
    -Wno-unused-function    # 禁用未使用的函数警告
    -Wno-unused-but-set-variable   # 禁用未使用但被设置的变量警告
    # -Wno-format  # 禁用格式化字符串警告
    -Wno-comment   # 禁用多行注释警告
    -Wno-pedantic  # 禁用不严谨的警告 如: 空文件等
    -fexec-charset=gbk   # 编译源文件字符集为 utf-8 or gbk
)
string(JOIN " " CUSTOM_C_CXX_FLAGS_STR ${CUSTOM_C_CXX_FLAGS})


set(CUSTOM_LINK_FLAGS 
    # -Wl,--no-warn-rwx-segments
    # -lnosys
    # -Wl,--wrap,malloc # 不使用系统malloc函数
    # -Wl,--wrap,free  # 不使用系统free函数
    # -Wl,--wrap,calloc # 不使用系统calloc函数
    # -Wl,--wrap,realloc # 不使用系统realloc函数
)
string(JOIN " " CUSTOM_LINK_FLAGS_STR ${CUSTOM_LINK_FLAGS})

# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CUSTOM_C_CXX_FLAGS_STR}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CUSTOM_C_CXX_FLAGS_STR}")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} ${CUSTOM_LINK_FLAGS_STR}")
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} ${CUSTOM_LINK_FLAGS_STR}")
